Thread: [Help] A Question About Template Specialization

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    [Help] A Question About Template Specialization

    compiled the following code
    Code:
    class t {
    public:
       template <typename T>
       void tt(T i) { }
       template <>
       void tt(int i) { }
    };
    
    int main()
    {
    }
    and I got the following error message
    5 invalid explicit specialization before '>' token
    5 explicit specialization in non-namespace scope `class t'
    6 invalid member function declaration
    but if I moved function tt out of the class t, say,
    Code:
    template <typename T>
    void tt(T i) { }
    template <>
    void tt(int i) { }
    
    int main()
    {
    }
    then it compiled well.

    I wonder if C++ dosen't support such kind of specialization inside a class? Why? For any good reason?

    Anyway, I do need to declare a template function, and a special version of this function, inside a normal class. How? The only alternative is to make it an overloaded function? say,
    Code:
    class t {
    public:
       template <typename T>
       void tt(T i) { }
    
       void tt(int i) { }
    };
    
    int main()
    {
    }
    then it compiled well
    Last edited by Antigloss; 10-31-2007 at 08:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. Template specialization + linking error
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 06-06-2008, 12:03 PM
  3. Function template specialization?
    By cpjust in forum C++ Programming
    Replies: 17
    Last Post: 02-25-2008, 04:11 PM
  4. Template Partial Specialization
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2007, 11:05 AM
  5. Template specialization
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-19-2002, 07:08 AM